package com.rbcs.sharing.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rbcs.sharing.model.Sharing;
import com.rbcs.sharing.model.SharingItem;
import com.systop.core.service.BaseGenericsManager;

@Service
public class SharingManager extends BaseGenericsManager<Sharing> {
	
	@Transactional
	public void remove(Sharing sharing){
		if (sharing != null && sharing.getId() != null){
			sharing = get(sharing.getId());
			for(SharingItem item : sharing.getSharingItems()){
				getDao().delete(item);
			}
			getDao().delete(sharing);
		}
	}
	
	@Transactional
	public void countYearSharing(Sharing sharing){
		if (sharing != null){
			Double amount = 0.0;
			for (SharingItem sitem : sharing.getSharingItems()){
				amount = amount + sitem.getAmount();
			}
			sharing.setAmount(amount);
			save(sharing);
		}
		
	}
}
